// pages/shoporder/shoporder.js
var util = require('../../../utils/util.js');
var textspeech=require('../../../utils/textspeech.js')
let socketOpen = false
var socketMsgQueue = []
Page({
  data: {
    msg: 'www',
    currentTab: 0,
    orderstate: '-1',
    orders: [],
    remindorders:[],//预定订单，到时自动提醒
    pagenum: 1,
    pagesize: 10,
    states: [],
    //提前提醒时间 （分）
    advanceTime:10,

    date: '',
    startdate:'2019-01-01',
    enddate:'2019-12-18',
    yearmonth: [[2019], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
    yearindex: 0,
    monthindex: 0

  },
  onLoad: function(options) {
    var date = new Date();
    var year = date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var param = year + "-" + month+"-"+day;
    this.setOrder(param);
    this.setstatename();
    this.setData({ date: param})
    //this.getneworder();
    //this.setData({ currentTab: 0, orderstate:0})
    //this.timetoremind();
  },
  swichNav: function (e) {

    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        orderstate: e.target.dataset.current
      })
    }
  },
  setOrder: function(date) {
    var that = this
    var url = util.url.shop_order_list;
    if (date) url += "?date=" + date;
    util.fetchGet(
      url,
      function(ret) {
        var list = ret.data;
        that.setOrderToData(list);
      } 
    )
  },
  sortFun:function(a,b){   
      if (b.fetchTime > a.fetchTime) {
        return 1;
      } else {
        return -1
      }   
  },
  //从新排序后放入订单列表
  setOrderToData:function(list){
    var that=this;
    var iswill=false;
      //已完成最下，根据fetchtime 排序  未到取餐时间的放在下面     
      that.setData({
        orders: list,       
      })
    
  },
  setstatename: function() {
    var that = this
    util.fetchGet(
      util.url.shop_order_orderstate,
      function(ret) {
        var map = ret.data;
        var states = [];
        for (var p in map) {
          var state = {
            "name": map[p],
            "id": p
          };
          states.push(state);
        }
        that.setData({
          states: states
        })
        console.log(ret);
      }
    )
  },
  
  getOrderYearMonth: function (year, month) {

    var day = 30;
    if (month in [1, 3, 5, 7, 8, 10, 12]) {
      day = 31
    } else if (month in [4, 6, 9, 11]) {
      day = 30
    } else {
      if (year % 4 == 0) {
        day = 29;
      } else {
        day = 28;
      }
    }
    if (month < 10) month = "0" + month;
    var date = year + "-" + month 
    this.setData({
      date: year + "-" + month
    })
    this.setOrder(startTime, util.url.order_history);
  },
  bindDateChange:function(e){
    this.setData({
      date: e.detail.value
    })
    this.setOrder(e.detail.value);
  }

  
}) 